subroutine r2abij_t2efij_t1bm_iefam_ri2(r2, t2, t1, & 
               icore, fact, rivv, rivo, rioo, ig, giop)
!
use mod_ri
use mod_ioff
use mod_iop
use mod_orbit
use mod_size
use mod_symm
implicit none
!
real*4, intent(in) :: r2(*), ig(*)
real*4, intent(in) :: t1(*), rivv(*), rivo(*), rioo(*)
!
real*4, intent(inout) :: t2(*), icore(*)
integer, intent(in) :: giop(5)
!
integer :: i, j, a, b, nsize, itmp
integer :: i0, i1, i2, i3, i4, nvvo, irrep
integer :: i0g, i1g, i2g, i3g, i4g, i5g
real*8 :: fact
integer, external :: idsymsoc
real*8, external :: dnrm2, nrm2so
!this is to do r2(ab,ij) = r2(abij) - P(ab)*0.5*t2(efij)*iabci(ef,am)*t1(bm)
nvvo = 1
do irrep = 1, nirrep 
    nvvo = max(nvvo, idsymsoc(irrep, vrta, vrta, 1, popa, zorb, 1), & 
                 idsymsoc(irrep, vrtb, vrtb, 1, popa, zorb, 1), & 
                 idsymsoc(irrep, vrta, vrta, 1, popb, zorb, 1), & 
                 idsymsoc(irrep, vrtb, vrtb, 1, popb, zorb, 1))
enddo
i0 = 1           
i1 = i0 + nvvoo*isd 
i2 = i1 + nvvoo*isd    
i3 = i2 + nvvoo*isd
i4 = i3 + nvvoo*isd
!
!====AAAA
if(giop(2)==2) then 
    igpu = 1 
    i0g = 1 
    i1g = i0g + nioi31*isd
    i2g = i1g + nioiri_vv1*isd
    i3g = i2g + nioiri_vo1*isd
    i4g = i3g + nioi31*isd
    i5g = i4g + nvvv*isd*2
    call myicopy_g(t2(ioi3(1))      , ig(i0g), nioi31    )
    call myicopy_g(rivv(ioiri_vv(1)), ig(i1g), nioiri_vv1)
    call myicopy_g(rivo(ioiri_vo(1)), ig(i2g), nioiri_vo1)
    call VmnpqVmnrs_to_Vpqrs_uri_vo2(ig(i0g), ig(i4g), 1, 1, vrta, vrta, 0, &
                  popa, popa, 0, vrta, popa, 1, ig(i3g), 1,  1.d0, 0.d0, &
                  ig(i1g), ig(i2g), 1) 
    call myicopy_c(ig(i3g), icore(i0), nvvoo)
    igpu = 0
    nsize = idsymsoc(1, popa, popa, 0, vrta, popa, 1)
    write(6,*)'norm of tmp', nrm2so(nsize, icore(i0), 1)
 elseif(giop(2)==3) then
    igpu = 1 
    i0g = 1
    i1g = i0g + nvvo*isd
    i2g = i1g + nioiri_vv1*isd
    i3g = i2g + nioiri_vo1*isd
    i4g = i3g + nvvo*isd
    i5g = i4g + nvvv*isd*2
    call myicopy_g(rivv(ioiri_vv(1)), ig(i1g), nioiri_vv1)
    call myicopy_g(rivo(ioiri_vo(1)), ig(i2g), nioiri_vo1)
    call VmnpqVmnrs_to_Vpqrs_uri_vo_l(ig(i0g), ig(i4g), 1, 1, vrta, vrta, 0, &
                  popa, popa, 0, vrta, popa, 1, ig(i3g), 1,  1.d0, 0.d0, &
                  ig(i1g), ig(i2g),  t2(ioi3(1)), icore(i0), icore(i3), 1 )
    igpu = 0
    nsize = idsymsoc(1, popa, popa, 0, vrta, popa, 1)
    write(6,*)'norm of tmp', nrm2so(nsize, icore(i0), 1)
 endif
!   call VmnpqVmnrs_to_Vpqrs_uri_vo2(t2(ioi3(1)), icore(i1), 1, 1, vrta, vrta, 0, &
!                 popa, popa, 0, vrta, popa, 1, icore(i0), 1,  1.d0, 0.d0, &
!                 rivv(ioiri_vv(1)), rivo, 1) 
call VpqrmVsm_to_Vpqrs(icore(i0), t1(ifvo(1)), 1, 1, popa, &
                popa, popa, 0, vrta, vrta, icore(i1), 1, 1.d0, 0.d0, 1) 
call iassymso(icore(i1), icore(i0), 1, popa, popa, vrta, vrta, 0)
call itranspso(icore(i0), icore(i1), popa, popa, 0, vrta, vrta, 0, 1)
call axpyso(nioi31, fact*2.d0, icore(i1), 1, r2(ioi3(1)), 1)
!====BBBB
 if(giop(2)==2) then 
    igpu = 1 
    i0g = 1 
    i1g = i0g + nioi33*isd
    i2g = i1g + nioiri_vv2*isd
    i3g = i2g + nioiri_vo2*isd
    i4g = i3g + nioi33*isd
    i5g = i4g + nvvv*isd*2
    call myicopy_g(t2(ioi3(3))      , ig(i0g), nioi33    )
    call myicopy_g(rivv(ioiri_vv(2)), ig(i1g), nioiri_vv2)
    call myicopy_g(rivo(ioiri_vo(2)), ig(i2g), nioiri_vo2)
    call VmnpqVmnrs_to_Vpqrs_uri_vo2(ig(i0g), ig(i4g), 1, 1, vrtb, vrtb, 0, &
               popb, popb, 0, vrtb, popb, 1, ig(i3g), 1,  1.d0, 0.d0,&
               ig(i1g), ig(i2g), 2) 
    call myicopy_c(ig(i3g), icore(i0), nvvoo)
    nsize = idsymsoc(1, popa, popa, 0, vrta, popa, 1)
    igpu = 0
 elseif(giop(2) == 3)  then  
    igpu = 1 
    i0g = 1
    i1g = i0g + nvvo*isd
    i2g = i1g + nioiri_vv2*isd
    i3g = i2g + nioiri_vo2*isd
    i4g = i3g + nvvo*isd
    i5g = i4g + nvvv*isd*2
    call myicopy_g(rivv(ioiri_vv(2)), ig(i1g), nioiri_vv2)
    call myicopy_g(rivo(ioiri_vo(2)), ig(i2g), nioiri_vo2)
    call VmnpqVmnrs_to_Vpqrs_uri_vo_l(ig(i0g), ig(i4g), 1, 1, vrtb, vrtb, 0, &
               popb, popb, 0, vrtb, popb, 1, ig(i3g), 1,  1.d0, 0.d0,&
               ig(i1g), ig(i2g), t2(ioi3(3)), icore(i0), icore(i3), 2) 
 endif
 igpu = 0
!call VmnpqVmnrs_to_Vpqrs_uri_vo2(t2(ioi3(3)), icore(i1), 1, 1, vrtb, vrtb, 0, &
!              popb, popb, 0, vrtb, popb, 1, icore(i0), 1,  1.d0, 0.d0,&
!              rivv(ioiri_vv(2)), rivo(ioiri_vo(2)), 2) 
call VpqrmVsm_to_Vpqrs(icore(i0), t1(ifvo(2)), 1, 1, popb, &
                popb, popb, 0, vrtb, vrtb, icore(i1), 1, 1.d0, 0.d0, 1) 
call iassymso(icore(i1), icore(i0), 1, popb, popb, vrtb, vrtb, 0)
call itranspso(icore(i0), icore(i1), popb, popb, 0, vrtb, vrtb, 0, 1)
call axpyso(nioi33, fact*2.d0, icore(i1), 1, r2(ioi3(3)), 1)
!
!====ABAB
!----t2(Ef,Ij)*iabci(Ef,Am)*t1(bm) + t2(Fe,Ij)*iabci(eF,bM)*t1(AM)
 if(giop(2)==2) then 
    igpu = 1 
    i0g = 1 
    i1g = i0g + nioi32*isd
    i2g = i1g + nioiri_vv1*isd
    i3g = i2g + nioiri_vo2*isd
    i4g = i3g + nioi32*isd
    i5g = i4g + nvvv*isd*2
    call myicopy_g(t2(ioi3(2))      , ig(i0g), nioi32    )
    call myicopy_g(rivv(ioiri_vv(1)), ig(i1g), nioiri_vv1)
    call myicopy_g(rivo(ioiri_vo(2)), ig(i2g), nioiri_vo2)
    call VmnpqVmnrs_to_Vpqrs_uri_vo2(ig(i0g), ig(i4g), 1, 1, vrta, vrtb, 1, &
              popa, popb, 1, vrta, popb, 1, ig(i3g), 1,  1.d0, 0.d0,&
              ig(i1g), ig(i2g), 3) 
    call myicopy_c(ig(i3g), icore(i0), nvvoo)
    igpu = 0
 elseif(giop(2)==3) then 
    igpu = 1 
    i0g = 1 
    i1g = i0g + nvvo*isd
    i2g = i1g + nioiri_vv1*isd
    i3g = i2g + nioiri_vo2*isd
    i4g = i3g + nvvo*isd
    i5g = i4g + nvvv*isd*2
    call myicopy_g(rivv(ioiri_vv(1)), ig(i1g), nioiri_vv1)
    call myicopy_g(rivo(ioiri_vo(2)), ig(i2g), nioiri_vo2)
    call VmnpqVmnrs_to_Vpqrs_uri_vo_l(ig(i0g), ig(i4g), 1, 1, vrta, vrtb, 1, &
              popa, popb, 1, vrta, popb, 1, ig(i3g), 1,  1.d0, 0.d0,&
              ig(i1g), ig(i2g), t2(ioi3(2)), icore(i0), icore(i3), 3) 
    igpu = 0
 endif
!call VmnpqVmnrs_to_Vpqrs_uri_vo2(t2(ioi3(2)), icore(i1), 1, 1, vrta, vrtb, 1, &
!              popa, popb, 1, vrta, popb, 1, icore(i0), 1,  1.d0, 0.d0,&
!              rivv(ioiri_vv(1)), rivo(ioiri_vo(2)), 3) 
call VpqrmVsm_to_Vpqrs(icore(i0), t1(ifvo(2)), 1, 1, popb, &
                popa, popb, 1, vrta, vrtb, icore(i2), 1, 1.d0, 0.d0, 1)
!
call isymtrso(t2(ioi3(2)), icore(i1), vrta,vrtb, 1, 1, popa, popb, 1, 0, 1)

igpu = 1 
 if(giop(2)==2) then 
    i0g = 1 
    i1g = i0g + nioi32*isd
    i2g = i1g + nioiri_vv2*isd
    i3g = i2g + nioiri_vo1*isd
    i4g = i3g + nioi32*isd
    i5g = i4g + nvvv*isd*2
    call myicopy_g(icore(i1)      , ig(i0g), nioi32    )
    call myicopy_g(rivv(ioiri_vv(2)), ig(i1g), nioiri_vv2)
    call myicopy_g(rivo(ioiri_vo(1)), ig(i2g), nioiri_vo1)
    call VmnpqVmnrs_to_Vpqrs_uri_vo2(ig(i0g), ig(i4g), 1, 1, vrtb, vrta, 1, &
              popa, popb, 1, vrtb, popa, 1, ig(i3g), 1,  1.d0, 0.d0, & 
              ig(i1g), ig(i2g), 4) 
    call myicopy_c(ig(i3g), icore(i0), nvvoo)
 elseif(giop(2)==3) then 
    i0g = 1 
    i1g = i0g + nvvo*isd
    i2g = i1g + nioiri_vv2*isd
    i3g = i2g + nioiri_vo1*isd
    i4g = i3g + nvvo*isd
    i5g = i4g + nvvv*isd*2
    call myicopy_g(icore(i1)      , ig(i0g), nioi32    )
    call myicopy_g(rivv(ioiri_vv(2)), ig(i1g), nioiri_vv2)
    call myicopy_g(rivo(ioiri_vo(1)), ig(i2g), nioiri_vo1)
    call VmnpqVmnrs_to_Vpqrs_uri_vo_l(ig(i0g), ig(i4g), 1, 1, vrtb, vrta, 1, &
              popa, popb, 1, vrtb, popa, 1, ig(i3g), 1,  1.d0, 0.d0, & 
              ig(i1g), ig(i2g), icore(i1), icore(i0), icore(i3), 4) 
 endif
igpu = 0
!call VmnpqVmnrs_to_Vpqrs_uri_vo2(t2(ioi3(2)), icore(i1), 1, 1, vrta, vrtb, 1, &
igpu = 0
!call VmnpqVmnrs_to_Vpqrs_uri_vo2(t2(ioi3(2)), icore(i1), 1, 1, vrta, vrtb, 1, &
!call VmnpqVmnrs_to_Vpqrs_uri_vo2(icore(i1), icore(i3), 1, 1, vrtb, vrta, 1, &
!              popa, popb, 1, vrtb, popa, 1, icore(i0), 1,  1.d0, 0.d0, & 
!              rivv(ioiri_vv(2)), rivo(ioiri_vo(1)), 4) 
call VpqrmVsm_to_Vpqrs(icore(i0), t1(ifvo(1)), 1, 1, popa, &
                popa, popb, 1, vrtb, vrta, icore(i1), 1, 1.d0, 0.d0, 1)
call isymtrso(icore(i1), icore(i0), popa, popb, 1, 0, vrtb, vrta, 1, 1, 1)
call axpyso(nioi32, 1.d0, icore(i0), 1, icore(i2), 1)
!
call itranspso(icore(i2), icore(i1), popa, popb, 1, vrta, vrtb, 1, 1) 
call axpyso(nioi32, fact*2.d0, icore(i1), 1, r2(ioi3(2)), 1)

!---- 

return
end
